<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="40%"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="handleClose"
  >
    <el-form :model="form" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="所属图书馆" prop="library_id">
        <el-select
          v-model="form.library_id"
          placeholder="请选择图书馆"
          style="width: 100%"
          filterable
          @change="handleLibraryChange"
        >
          <el-option
            v-for="item in libraryOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属楼层" prop="floor_id">
        <el-select
          v-model="form.floor_id"
          placeholder="请选择楼层"
          style="width: 100%"
          filterable
          @change="handleFloorChange"
        >
          <el-option
            v-for="item in floorOptions"
            :key="item.id"
            :label="item.floor_number"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属区域" prop="area_id">
        <el-select
          v-model="form.area_id"
          placeholder="请选择区域"
          style="width: 100%"
          filterable
        >
          <el-option
            v-for="item in areaOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="座位名称" prop="name">
        <el-input v-model="form.name" placeholder="例如：A01、B02" />
      </el-form-item>
      <el-form-item label="行索引" prop="row_index">
        <el-input-number v-model="form.row_index" :min="0" :max="99" />
      </el-form-item>
      <el-form-item label="列索引" prop="column_index">
        <el-input-number v-model="form.column_index" :min="0" :max="99" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status" style="width: 100%">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          type="textarea"
          :rows="3"
          placeholder="请输入备注信息"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { http } from "@/api";

export default {
  name: "SeatAdd",
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "新增座位",
      isEdit: false,
      form: {
        id: undefined,
        library_id: undefined,
        floor_id: undefined,
        area_id: undefined,
        name: "",
        row_index: 0,
        column_index: 0,
        status: "available",
        remark: ""
      },
      rules: {
        library_id: [
          { required: true, message: "请选择所属图书馆", trigger: "change" }
        ],
        floor_id: [
          { required: true, message: "请选择所属楼层", trigger: "change" }
        ],
        area_id: [
          { required: true, message: "请选择所属区域", trigger: "change" }
        ],
        name: [
          { required: true, message: "座位名称不能为空", trigger: "blur" },
          { max: 50, message: "座位名称不能超过50个字符", trigger: "blur" }
        ],
        status: [
          { required: true, message: "请选择状态", trigger: "change" }
        ],
        remark: [
          { max: 255, message: "备注不能超过255个字符", trigger: "blur" }
        ]
      },
      libraryOptions: [],
      floorOptions: [],
      areaOptions: [],
      statusOptions: [
        { label: "可用", value: "available" },
        { label: "禁用", value: "disabled" }
      ]
    };
  },
  methods: {
    open(row) {
      this.dialogVisible = true;
      this.getLibraryOptions();
      
      if (row) {
        this.isEdit = true;
        this.dialogTitle = "编辑座位";
        this.form = { ...row };
        
        // 加载楼层和区域选项
        this.handleLibraryChange();
        this.handleFloorChange();
      } else {
        this.isEdit = false;
        this.dialogTitle = "新增座位";
        this.form = {
          id: undefined,
          library_id: undefined,
          floor_id: undefined,
          area_id: undefined,
          name: "",
          row_index: 0,
          column_index: 0,
          status: "available",
          remark: ""
        };
      }
      
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    
    getLibraryOptions() {
      http.libraries.list().then(response => {
        this.libraryOptions = response.data;
      });
    },
    
    handleLibraryChange() {
      this.form.floor_id = undefined;
      this.form.area_id = undefined;
      this.floorOptions = [];
      this.areaOptions = [];
      
      if (this.form.library_id) {
        http.libraryFloors.list({ library_id: this.form.library_id }).then(response => {
          this.floorOptions = response.data;
        });
      }
    },
    
    handleFloorChange() {
      this.form.area_id = undefined;
      this.areaOptions = [];
      
      if (this.form.floor_id) {
        http.libraryAreas.list({ floor_id: this.form.floor_id }).then(response => {
          this.areaOptions = response.data;
        });
      }
    },
    
    handleClose() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
    },
    
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            if (this.isEdit) {
              await http.seats.update(this.form.id, this.form);
              this.$message.success("更新成功");
            } else {
              await http.seats.create(null, this.form);
              this.$message.success("新增成功");
            }
            this.handleClose();
            this.$emit("get-page");
          } catch (error) {

          }
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.el-dialog {
  max-height: 90vh;
  overflow: auto;
}
.el-dialog__body {
  max-height: calc(90vh - 120px);
  overflow-y: auto;
}
</style>
